package com.suse.lhw.resourceservere.controller;

import com.sun.javafx.util.Logging;
import com.suse.lhw.domin.FUnit;
import com.suse.lhw.hms.foodserver.pojo.input_POJO.ModelUnitAndIfo;
import com.suse.lhw.resourceservere.POJO.input_POJO.addAFoodModel;
import com.suse.lhw.resourceservere.POJO.input_POJO.modelAndUnits;
import com.suse.lhw.resourceservere.POJO.input_POJO.model_ifo_units;
import com.suse.lhw.resourceservere.client.foodserverClient;
import com.suse.lhw.resourceservere.utils.FileUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.runtime.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.resourceservere.com.suse.lhw.security.controller
 * @ClassName: FoodController
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/5/30 11:28
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/30 11:28
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Controller
@RequestMapping("fooduser")
public class FoodUserController {
    @Autowired
    foodserverClient foodserverClient;

    @RequestMapping("sayhello")
    public String sayhello() {
        return foodserverClient.hello();
    }


    @RequestMapping(value = "testFileUpload", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
    public Map<String, Object> testFileUpload(@RequestPart("afile") MultipartFile file) {
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println(file.toString());
        String filename = FileUploader.uploadAImg(file);
        map.put("returnMsg", filename);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "addAUnuploadedFoodModel"
//            ,method=RequestMethod.POST,consumes=MULTIPART_FORM_DATA_VALUE
    )
    public Map<String, Object> addAUnuploadedFoodModel(@RequestParam(required = false) String name, @RequestParam(required = false) String fmdescription, @RequestParam(required = false) String ifodescroption, @RequestParam(required = false) String ortherNutrient,
                                                       @RequestParam(required = false) Double carbohydrate, @RequestParam(required = false) Double protein, @RequestParam(required = false) String tag, @RequestParam(required = false) Double fat,
                                                       @RequestParam(required = false) String nameString, @RequestParam(required = false) String numString
            , @RequestPart("afile") MultipartFile file
    ) {
        System.out.println("the namestring is" + nameString + "the numstring is" + numString);
        Map<String, Object> map = new HashMap<String, Object>();
        //解析字符串当做单位，字符串以@为分隔符
        String[] unitnumber = numString.split("@");
        String[] unitname = nameString.split("@");
        List<FUnit> list = new ArrayList<FUnit>();
        for (int i = 0; i < unitnumber.length; i++) {
            FUnit fUnit = new FUnit();
            fUnit.setUnitName(unitname[i]);
            fUnit.setUnitConsumeCalories(Integer.parseInt(unitnumber[i]));
            list.add(fUnit);
            System.out.println("the funit is" + fUnit.toString());
        }
        //现在使用写好的工具人上传文件，controller提交事务不太行，我也不想把文件传来传去，如果controller失败了还上传了文件没删，就当运气不好,~o( =∩ω∩= )m
        String filename = FileUploader.uploadAImg(file);
        model_ifo_units m = new model_ifo_units(name, filename, fmdescription, ifodescroption, carbohydrate, protein, fat, tag, ortherNutrient, list);
        System.out.println("the m is" + m.toString());
        String result = foodserverClient.addAUnuploadedFoodModel(m);
        map.put("returnMsg", result);
        return map;
    }


    @ResponseBody
    @RequestMapping("selectFoodModelByNameUseridStatus")
    public Map<String, Object> selectFoodModelByNameUseridStatus(@RequestParam(required = false) int status, @RequestParam(required = false) String name) {
        String userId = "aaa";
        Map<String, Object> map = new HashMap<>(2);
        System.out.println("name is" + name + "status" + status);
        List<modelAndUnits> list = new ArrayList<modelAndUnits>();
        list = foodserverClient.selectModelByNameStatusUserid(name, status, userId);
        if (list == null) {
            System.out.println("没找到！！！！！");
            return map;
        }
        System.out.println("不为空");
        for (modelAndUnits modelAndUnits : list) {
            System.out.println("modelAndUnits are : " + modelAndUnits.toString());
        }
        map.put("returnMsg", "一共找到" + list.size() + "条数据！");
        map.put("returnList", list);
        System.out.println("返回的list是" + list);
        return map;
    }
//@ResponseBody
//@RequestMapping("addAUnuploadedFoodModel")
//public String addAUnuploadedFoodModel(Model model, @RequestParam(required = false) addAFoodModel pojo,@RequestBody List<FUnit> list){
//    System.out.println("pojo"+pojo+"list"+list);
//    String result=foodserverClient.addAUnuploadedFoodModel(model,pojo,list);
//    return result;
//}

    @ResponseBody
    @RequestMapping("eatAFoodModelById")
    public Map<String, Object> eatAFoodModelById(@RequestParam int foodId, @RequestParam int unitId, @RequestParam int quanity, @RequestParam int eattime) {
        /**
         * @Author:Hongwei Li
         * @Description:用户吃掉一个食品模型
         * @Date:16:29 2020/6/2
         * @Param:[foodId, unitId, quanity, eattime]
         * @Return:java.util.Map<java.lang.String,java.lang.Object>
         **/
        System.out.println("前端传来的值是：" + foodId + unitId + quanity + eattime);
        Map<String, Object> map = new HashMap<>();
        String returnMsg = foodserverClient.eatAFoodByModelId(foodId, unitId, quanity, eattime);
        if (returnMsg == null) {
            returnMsg = "没吃掉！";
        }
        System.out.println("！！！！前端传来的参数是：" + returnMsg);
        map.put("returnMsg", returnMsg);
        return map;

    }

    @ResponseBody
    @RequestMapping("selectMUIByMid")
    public Map<String, Object> selectMUIByMid(@RequestParam int foodId) {
        System.out.println("foodid is" + foodId);
        Map<String, Object> map = new HashMap<>();
        ModelUnitAndIfo modelUnitAndIfo = foodserverClient.findAFoodModelById(foodId);
        if (modelUnitAndIfo == null) {
            System.out.println("??????");
            String returnMsg = "查看出错了";
            map.put("ErrorMsg", returnMsg);
        } else {
            map.put("mui", modelUnitAndIfo);
            map.put("url", "takealookoffood");
        }
        System.out.println(modelUnitAndIfo.toString());
        return map;
    }

    @ResponseBody
    @RequestMapping("deleteFoodInstanse")
    public Map<String, Object> deleteFoodInstanse(@RequestParam int FoodInstanseId) {
        System.out.println("this foodid is " + FoodInstanseId);
        String resutl = foodserverClient.splitOutAFoodByInstanceId(FoodInstanseId);
        System.out.println("the result is" + resutl);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("returnMsg", resutl);
        return map;
    }

    @ResponseBody
    @RequestMapping("deleteAFoodModel")
    public Map<String, Object> deleteAFoodModel(@RequestParam int foodid) {
        ModelUnitAndIfo modelUnitAndIfo = foodserverClient.findAFoodModelById(foodid);
        String result = foodserverClient.deleteAFoodModel(foodid);
        //开始删除文件（图片）
        if (result.equals("满门抄斩成功！")) {
            File file = new File("D:\\AppFilesWithNoPriority\\毕业设计\\项目\\减肥管理系统\\health_manage_system\\resourceservere\\src\\main\\resources\\static\\image\\userImg\\" + modelUnitAndIfo.getImgurl());
            if (file.delete()) {
                System.out.println("文件删除成功");
            } else {
                System.out.println("文件删除失败！！！！");
            }
        }
        System.out.println("the result is " + result);
        Map<String, Object> map = new HashMap<>();
        map.put("returnMsg", result);
        return map;
    }


}
